package com.ss.android.image;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.facebook.common.internal.Preconditions;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.drawable.DrawableParent;
import com.facebook.drawee.drawable.ScaleTypeDrawable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.WrappingUtils;
import com.facebook.drawee.interfaces.DraweeHierarchy;
import com.facebook.drawee.interfaces.SimpleDraweeControllerBuilder;
import com.facebook.drawee.view.SimpleDraweeView;

public class AsyncImageView extends SimpleDraweeView {
    public AsyncImageView(Context context) {
        super(context);
        init();
    }

    public AsyncImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AsyncImageView(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        init();
    }

    public AsyncImageView(Context context, GenericDraweeHierarchy genericDraweeHierarchy) {
        super(context, genericDraweeHierarchy);
        init();
    }

    private void init() {
        DraweeHierarchy draweeHierarchy = getHierarchy();
        if (draweeHierarchy != null) {
            ((GenericDraweeHierarchy) draweeHierarchy).mFadeDrawable.setTransitionDuration(200);
        }
    }

    public void setImageURI(Uri uri, @Nullable Object obj) {
        setController(getControllerBuilder().setCallerContext(obj).setUri(uri).build());
    }

    public final void a(Image image, int arg5, int arg6) {
        if (image != null && image.isLocal() && arg6 > 0 && arg5 > 0) {
            SimpleDraweeControllerBuilder controllerBuilder = getControllerBuilder();
            if ((controllerBuilder instanceof AbstractDraweeControllerBuilder)) {
                AbstractDraweeControllerBuilder absControllerBuilder = (AbstractDraweeControllerBuilder) controllerBuilder;
                absControllerBuilder.setOldController(getController())
                        .setAutoPlayAnimations()
                        .setFirstAvailableImageRequests(FrescoUtils.buildImageRequests(image, arg5, arg6));
            }
            setController(controllerBuilder.build());
            setVisibility(getVisibility());
        }
    }

    public void setImage(Image image, BaseControllerListener controllerListener) {
        SimpleDraweeControllerBuilder simpleDraweeControllerBuilder = getControllerBuilder();
        if (simpleDraweeControllerBuilder instanceof AbstractDraweeControllerBuilder) {
            AbstractDraweeControllerBuilder controllerBuilder = (AbstractDraweeControllerBuilder) simpleDraweeControllerBuilder;
            controllerBuilder
                    .setOldController(getController())
                    .setControllerListener(controllerListener)
                    .setAutoPlayAnimations()
                    .setFirstAvailableImageRequests(FrescoUtils.buildImageRequests(image));
        }
        setController(simpleDraweeControllerBuilder.build());
        setVisibility(getVisibility());
    }

    public SimpleDraweeControllerBuilder getControllerBuilder() {
        SimpleDraweeControllerBuilder controllerBuilder = super.getControllerBuilder();
        if ((controllerBuilder instanceof AbstractDraweeControllerBuilder)) {
            AbstractDraweeControllerBuilder absControllerBuilder = (AbstractDraweeControllerBuilder) controllerBuilder;
            absControllerBuilder.init();
            absControllerBuilder.getThis();
        }
        controllerBuilder.setOldController(getController());
        return controllerBuilder;
    }

    public void setActualImageScaleType(ScalingUtils.ScaleType scaleType) {
        GenericDraweeHierarchy draweeHierarchy = (GenericDraweeHierarchy) getHierarchy();
        Preconditions.checkNotNull(scaleType);
        DrawableParent drawableParent = draweeHierarchy.getParentDrawableAtIndex(draweeHierarchy.ACTUAL_IMAGE_INDEX);
        if (!(drawableParent instanceof ScaleTypeDrawable)) {
            drawableParent = WrappingUtils.a(drawableParent, ScalingUtils.ScaleType.FIT_XY);
        }
        ScaleTypeDrawable scaleTypeDrawable = (ScaleTypeDrawable) drawableParent;
        scaleTypeDrawable.mScaleType = scaleType;
        scaleTypeDrawable.configureBounds();
        scaleTypeDrawable.invalidateSelf();
    }

    public void setImage(Image image) {
        setImage(image, null);
    }

    public void setPlaceHolderImage(int placeHolderImage) {
        ((GenericDraweeHierarchy) getHierarchy()).setChildDrawableAtIndex(placeHolderImage);
    }

    public void setUrl(String url) {
        Uri uri = url == null ? null : Uri.parse(url);
        setImageURI(uri);
        setVisibility(getVisibility());
    }
}

